package com.ming.elm.controller;


import com.ming.elm.po.Cart;
import com.ming.elm.service.CartService;
import com.ming.elm.service.impl.CartServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class CartController {

	private CartService cs = new CartServiceImpl();

	public Object saveCart(HttpServletRequest request) throws Exception{

		String str1 = request.getParameter("businessId");
		String str2 = request.getParameter("foodId");
		String str3 = request.getParameter("quantity");
		String userId = request.getParameter("userId");
		Integer businessId = Integer.valueOf(str1);
		Integer foodId = Integer.valueOf(str2);
		Integer quantity = Integer.valueOf(str3);

		Cart cart = new Cart();
		cart.setUserId(userId);
		cart.setBusinessId(businessId);
		cart.setQuantity(quantity);
		cart.setFoodId(foodId);

		int result = cs.saveCart(cart);

		return result;
	}
	
	public Object updateCart(HttpServletRequest request) throws Exception{

		String str1 = request.getParameter("businessId");
		String str2 = request.getParameter("foodId");
		String str3 = request.getParameter("quantity");
		String userId = request.getParameter("userId");
		Integer businessId = Integer.valueOf(str1);
		Integer foodId = Integer.valueOf(str2);
		Integer quantity = Integer.valueOf(str3);
		// 业务数据封装传给服务层
		Cart cart = new Cart();
		cart.setUserId(userId);
		cart.setBusinessId(businessId);
		cart.setQuantity(quantity);
		cart.setFoodId(foodId);

		int result = cs.updateCart(cart);


		return result;
	}
	
	public Object removeCart(HttpServletRequest request) throws Exception{

		String str1 = request.getParameter("businessId");
		String str2 = request.getParameter("foodId");
		String str3 = request.getParameter("quantity");
		String userId = request.getParameter("userId");
		Integer businessId = Integer.valueOf(str1);
		Integer foodId = Integer.valueOf(str2);
		Integer quantity = Integer.valueOf(str3);
		// 业务数据封装传给服务层
		Cart cart = new Cart();
		cart.setUserId(userId);
		cart.setBusinessId(businessId);
		cart.setQuantity(quantity);
		cart.setFoodId(foodId);

		int result = cs.removeCart(cart);

		return result;
	}
	
	public Object listCart(HttpServletRequest request) throws Exception{
		String str = request.getParameter("businessId");
		String userId = request.getParameter("userId");
		Integer businessId = null;
		if(str!=null){
			businessId = Integer.valueOf(str);
		}
		Cart cart = new Cart();
		cart.setBusinessId(businessId);
		cart.setUserId(userId);
		List<Cart> carts = cs.listCart(cart);

		return carts;
	}
}
